package de.tabuma.customer.controller;

import de.tabuma.customer.*;
import de.tabuma.customer.entity.*;
import de.tabuma.customer.controller.*;
import de.tabuma.customer.logik.*;
import de.tabuma.lib.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class createController extends tabumacontroller {

    private static final Logger log = Logger.getLogger(createController.class.getName());

    public createController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        request.setAttribute(constants.paramName, ""+constants.ACTION_CREATE);

        if(request.getParameter(constants.paramAdd)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            
            String customerName = (String) request.getParameter(constants.customerFieldName);
            Customer customer = new Customer();
            customer.setName(customerName);
            int id = CustomerFacade.create(customer, user.getEmail());
            CustomerUserRelation cur = new CustomerUserRelation();
            cur.setCostumerId(id);
            cur.setUser(user.getEmail());
            id = CustomerUserRelationFacade.create(cur, user.getEmail());

            request.setAttribute(constants.paramName, "" + constants.ACTION_LIST);

            Hashtable table = (Hashtable) this.getParameter().clone();
            listController listController = new listController(this.getServlet(), table);
            listController.navigate(context, request, response, out);
            
        }
    }
}